package com.file.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Collector;
import com.entity.Measuringpoint;
import com.entity.Meter;
import com.entity.Org;
import com.entity.Terminal;
import com.file.entity.FileMeasuringpoint;
import com.interfaces.CollectorDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.SysdicDao;
import com.interfaces.TerminalDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: MeasuringpointService.java
 * @Package com.service
 * @author 孙梦楠
 * @date 2017年7月25日 上午9:31:12
 * @version V1.0.0
 */

@Service
public class MeasuringpointService {

	private static final Logger log = LoggerFactory.getLogger(MeasuringpointService.class);

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private MeasuringpointDao measuringpointdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private CollectorDao collectordao;

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private OrgDao orgdao;

	// 列出所有采集点
	// @Transactional
	// public JsonResult<List<FileMeasuringpoint>> listmeasuringpoint(String
	// opeorgcookie, String opetypecookie,
	// String languagecookie) {
	//
	// List<Measuringpoint> measuringpoints =
	// measuringpointdao.findByOrg(Integer.valueOf(opeorgcookie),
	// opetypecookie);
	// List<FileMeasuringpoint> filemeasuringpoints = new
	// ArrayList<FileMeasuringpoint>();
	//
	// if (measuringpoints.size() != 0) {
	// for (int i = 0; i < measuringpoints.size(); i++) {
	//
	// Measuringpoint measuringpoint = measuringpoints.get(i);
	// filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint,
	// languagecookie));
	// }
	// }
	//
	// return new JsonResult<List<FileMeasuringpoint>>(182, "展示采集点所有列表",
	// filemeasuringpoints);
	// }

	// 根据终端ID列出所有采集点列表
	@Transactional
	public JsonResult<List<Measuringpoint>> listMeasuringpointByTerminalId(Integer terminalid) {

		List<Measuringpoint> list = measuringpointdao.findByTerminalid(terminalid);

		return new JsonResult<List<Measuringpoint>>(189, "根据终端ID列出所有采集点列表(实际值)", list);
	}

	// 条件列表
	@Transactional
	public JsonResult<List<FileMeasuringpoint>> listmeasuringpointByConditions(Integer terminalid, Integer meterid,
			Integer collectorid, String measuringpointnumber, String measuringpointordernumber,
			String measuringpointport, String measuringpointbaud, String measuringpointbigint,
			String measuringpointsmallint, String measuringpointint, String measuringpointdec, String opeorgcookie,
			String opetypecookie, String languagecookie) {

		// log.info("terminalid " + terminalid);
		// log.info("meterid " + meterid);
		// log.info("collectorid " + collectorid);
		// log.info("measuringpointnumber " + measuringpointnumber);
		// log.info("measuringpointordernumber " + measuringpointordernumber);
		// log.info("measuringpointport " + measuringpointport);
		// log.info("measuringpointbaud " + measuringpointbaud);
		// log.info("measuringpointbigint " + measuringpointbigint);
		// log.info("measuringpointsmallint " + measuringpointsmallint);
		// log.info("measuringpointint " + measuringpointint);
		// log.info("measuringpointdec " + measuringpointdec);
		// log.info("opeorgcookie " + opeorgcookie);

		if (terminalid == null && meterid == null && collectorid == null && Utils.isNull(measuringpointnumber)
				&& Utils.isNull(measuringpointordernumber) && Utils.isNull(measuringpointport)
				&& Utils.isNull(measuringpointbaud) && Utils.isNull(measuringpointbigint)
				&& Utils.isNull(measuringpointsmallint) && Utils.isNull(measuringpointint)
				&& Utils.isNull(measuringpointdec)) {
			List<FileMeasuringpoint> filemeasuringpoints = new ArrayList<FileMeasuringpoint>();

			if (opeorgcookie.equals("1")) {
				List<Measuringpoint> measuringpoints = measuringpointdao.findByType(opetypecookie);
				for (Measuringpoint measuringpoint : measuringpoints) {
					filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Measuringpoint> measuringpoints = measuringpointdao
							.findByOrg(Integer.parseInt(org.getOrgvalue()), opetypecookie);
					for (Measuringpoint measuringpoint : measuringpoints) {
						filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint, languagecookie));
					}
				}
			} else {

				List<Measuringpoint> measuringpoints = measuringpointdao.findByOrg(Integer.valueOf(opeorgcookie),
						opetypecookie);

				if (measuringpoints.size() != 0) {
					for (int i = 0; i < measuringpoints.size(); i++) {

						Measuringpoint measuringpoint = measuringpoints.get(i);
						filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint, languagecookie));
					}
				}
			}
			log.info("ListSuccess");
			return new JsonResult<List<FileMeasuringpoint>>(182, "展示采集点所有列表", filemeasuringpoints);

		} else {
			List<FileMeasuringpoint> filemeasuringpoints = new ArrayList<FileMeasuringpoint>();
			if (opeorgcookie.equals("1")) {
				List<Measuringpoint> measuringpoints = measuringpointdao.findByConditions(terminalid, meterid,
						collectorid, measuringpointnumber, measuringpointordernumber, measuringpointport,
						measuringpointbaud, measuringpointbigint, measuringpointsmallint, measuringpointint,
						measuringpointdec, null, opetypecookie);
				for (Measuringpoint measuringpoint : measuringpoints) {
					filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Measuringpoint> measuringpoints = measuringpointdao.findByConditions(terminalid, meterid,
							collectorid, measuringpointnumber, measuringpointordernumber, measuringpointport,
							measuringpointbaud, measuringpointbigint, measuringpointsmallint, measuringpointint,
							measuringpointdec, Integer.parseInt(org.getOrgvalue()), opetypecookie);
					for (Measuringpoint measuringpoint : measuringpoints) {
						filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint, languagecookie));
					}
				}
			} else {
				List<Measuringpoint> measuringpoints = measuringpointdao.findByConditions(terminalid, meterid,
						collectorid, measuringpointnumber, measuringpointordernumber, measuringpointport,
						measuringpointbaud, measuringpointbigint, measuringpointsmallint, measuringpointint,
						measuringpointdec, Integer.valueOf(opeorgcookie), opetypecookie);

				if (measuringpoints.size() != 0) {
					for (int i = 0; i < measuringpoints.size(); i++) {

						Measuringpoint measuringpoint = measuringpoints.get(i);
						filemeasuringpoints.add(dofilemeasuringpoint(measuringpoint, languagecookie));
					}
				}
			}
			log.info("ListByConditionsSuccess");
			return new JsonResult<List<FileMeasuringpoint>>(183, "展示采集点条件列表", filemeasuringpoints);
		}
	}

	// 增加
	@Transactional
	public JsonResult<Measuringpoint> addmeasuringpoint(Integer terminalid, Integer meterid, Integer collectorid,
			String measuringpointnumber, String measuringpointordernumber, String measuringpointport,
			String measuringpointbaud, String measuringpointbigint, String measuringpointsmallint,
			String measuringpointint, String measuringpointdec) {

		if (Utils.isNullOrEmpty(measuringpointnumber) || Utils.isNullOrEmpty(measuringpointordernumber)
				|| Utils.isNullOrEmpty(measuringpointport) || Utils.isNullOrEmpty(measuringpointbaud)
				|| Utils.isNullOrEmpty(measuringpointbigint) || Utils.isNullOrEmpty(measuringpointsmallint)
				|| Utils.isNullOrEmpty(measuringpointint) || Utils.isNullOrEmpty(measuringpointdec)
				|| terminalid == null || meterid == null) {

			return new JsonResult<Measuringpoint>(173, "增加采集点失败..数值不能为空", null);
		} else if (measuringpointdao.findByTerminalidAndMeasuringpointnumber(terminalid,
				measuringpointnumber) != null) {

			return new JsonResult<Measuringpoint>(174, "增加采集点失败..测量点号不能重复", null);
		} else if (measuringpointdao.findByTerminalidAndMeasuringpointordernumber(terminalid,
				measuringpointordernumber) != null) {

			return new JsonResult<Measuringpoint>(175, "增加采集点失败..测量点序号不能重复", null);
		} else {

			Measuringpoint measuringpoint = new Measuringpoint();
			measuringpoint.setTerminalid(terminalid);
			measuringpoint.setMeterid(meterid);
			measuringpoint.setCollectorid(collectorid);
			measuringpoint.setMeasuringpointnumber(measuringpointnumber);
			measuringpoint.setMeasuringpointordernumber(measuringpointordernumber);
			measuringpoint.setMeasuringpointport(measuringpointport);
			measuringpoint.setMeasuringpointbaud(measuringpointbaud);
			measuringpoint.setMeasuringpointbigint(measuringpointbigint);
			measuringpoint.setMeasuringpointsmallint(measuringpointsmallint);
			measuringpoint.setMeasuringpointint(measuringpointint);
			measuringpoint.setMeasuringpointdec(measuringpointdec);
			measuringpointdao.save(measuringpoint);
			log.info("AddMeasuringpointSuccess");
			// 更改集中器状态
			terminaldao.updateTerminal("3", terminalid);
			// 绑定之后更改表状态
			meterdao.updateMeter("3", meterid);
			// 更改采集器状态
			if (collectorid != null) {
				collectordao.updateCollector("3", collectorid);
			}

			return new JsonResult<Measuringpoint>(176, "增加采集点成功", measuringpoint);
		}
	}

	// 删除采集点
	@Transactional
	public JsonResult<Measuringpoint> deletemeasuringpoint(Integer measuringpointid) {

		if (measuringpointid != null) {

			// 改正表状态
			Integer meterid = measuringpointdao.findOne(measuringpointid).getMeterid();
			meterdao.updateMeter("2", meterid);
			// 如果采集器没有连接，更改采集器状态
			List<Measuringpoint> measuringpoints1 = measuringpointdao
					.findByCollectorid(measuringpointdao.findOne(measuringpointid).getCollectorid());
			if (measuringpoints1.size() == 1) {

				collectordao.updateCollector("1", measuringpointdao.findOne(measuringpointid).getCollectorid());

			}
			// 如果集中器没有连接，更改集中器状态
			List<Measuringpoint> measuringpoints2 = measuringpointdao
					.findByTerminalid(measuringpointdao.findOne(measuringpointid).getTerminalid());
			if (measuringpoints2.size() == 1) {

				terminaldao.updateTerminal("1", measuringpointdao.findOne(measuringpointid).getTerminalid());

			}

			measuringpointdao.delete(measuringpointid);

			return new JsonResult<Measuringpoint>(177, "删除采集点成功", null);
		}
		return new JsonResult<Measuringpoint>(186, "删除采集点失败..ID为空", null);
	}

	// 修改界面
	@Transactional
	public JsonResult<FileMeasuringpoint> updateviewmeasuringpoint(Integer measuringpointid, String languagecookie) {

		if (measuringpointid != null) {
			Measuringpoint measuringpoint = measuringpointdao.findOne(measuringpointid);

			return new JsonResult<FileMeasuringpoint>(184, "查单个采集点",
					dofilemeasuringpoint(measuringpoint, languagecookie));
		}
		return new JsonResult<FileMeasuringpoint>(185, "查单个采集点失败..ID为空", null);
	}

	// 通过表ID和采集终端ID获取采集点对象
	@Transactional
	public Measuringpoint findByTerminalidAndMeterid(Integer terminalid, Integer meterid) {

		Measuringpoint measuringpoint = measuringpointdao.findByTerminalidAndMeterid(terminalid, meterid);

		return measuringpoint;
	}

	// 通过表ID查终端ID
	public Integer findTerminalidByMeterid(Integer meterid) {

		Measuringpoint measuringpoint = measuringpointdao.findByMeterid(meterid);
		Integer terminalid = measuringpoint.getTerminalid();

		return terminalid;
	}

	// 修改
	@Transactional
	public JsonResult<Measuringpoint> updatemeasuringpoint(Integer measuringpointid, Integer terminalid,
			Integer meterid, Integer collectorid, String measuringpointnumber, String measuringpointordernumber,
			String measuringpointport, String measuringpointbaud, String measuringpointbigint,
			String measuringpointsmallint, String measuringpointint, String measuringpointdec) {

		log.info("====================collectorid   " + collectorid);

		Measuringpoint measuringpoint = new Measuringpoint();

		if (Utils.isEmpty(measuringpointnumber) || Utils.isEmpty(measuringpointordernumber)
				|| Utils.isEmpty(measuringpointport) || Utils.isEmpty(measuringpointbaud)
				|| Utils.isEmpty(measuringpointbigint) || Utils.isEmpty(measuringpointsmallint)
				|| Utils.isEmpty(measuringpointint) || Utils.isEmpty(measuringpointdec) || Utils.isEmpty(terminalid)
				|| Utils.isEmpty(collectorid) || Utils.isEmpty(meterid) || measuringpointid == null) {

			return new JsonResult<Measuringpoint>(178, "修改采集点失败..数值不能为空", null);

		} else {

			measuringpoint.setMeasuringpointid(measuringpointid);
			// 判断measuringpointnumber
			if (Utils.isNull(measuringpointnumber)) {
				measuringpoint
						.setMeasuringpointnumber(measuringpointdao.findOne(measuringpointid).getMeasuringpointnumber());

			} else if (terminalid == null // 传入terminalid的情况不同，通过不同方式获取terminalid
					&& measuringpointdao.findByTerminalidAndMeasuringpointnumber(
							(measuringpointdao.findOne(measuringpointid).getTerminalid()), measuringpointnumber) != null
					&& !(measuringpointdao.findOne(measuringpointid).getMeasuringpointnumber()
							.equals(measuringpointnumber))) {

				return new JsonResult<Measuringpoint>(179, "修改采集点失败..测量点号不能重复", null);
			} else if (terminalid != null // 传入terminalid的情况不同，通过不同方式获取terminalid
					&& measuringpointdao.findByTerminalidAndMeasuringpointnumber(terminalid,
							measuringpointnumber) != null
					&& !(measuringpointdao.findOne(measuringpointid).getMeasuringpointnumber()
							.equals(measuringpointnumber))) {

				return new JsonResult<Measuringpoint>(179, "修改采集点失败..测量点号不能重复", null);
			} else {

				measuringpoint.setMeasuringpointnumber(measuringpointnumber);
			}

			// 判断measuringpointordernumber
			if (Utils.isNull(measuringpointordernumber)) {
				measuringpoint.setMeasuringpointordernumber(
						measuringpointdao.findOne(measuringpointid).getMeasuringpointordernumber());

			} else if (terminalid == null
					&& measuringpointdao.findByTerminalidAndMeasuringpointordernumber(
							(measuringpointdao.findOne(measuringpointid).getTerminalid()),
							measuringpointordernumber) != null
					&& !(measuringpointdao.findOne(measuringpointid).getMeasuringpointordernumber()
							.equals(measuringpointordernumber))) {
				return new JsonResult<Measuringpoint>(180, "修改采集点失败..测量点序号不能重复", null);

			} else if (terminalid != null
					&& measuringpointdao.findByTerminalidAndMeasuringpointordernumber(terminalid,
							measuringpointordernumber) != null
					&& !(measuringpointdao.findOne(measuringpointid).getMeasuringpointordernumber()
							.equals(measuringpointordernumber))) {

				return new JsonResult<Measuringpoint>(180, "修改采集点失败..测量点序号不能重复", null);
			} else {

				measuringpoint.setMeasuringpointordernumber(measuringpointordernumber);
			}

//			// 解绑
//			// 先把要改的采集点的表状态改回2，再进行修改
//			Integer meterid_old = measuringpointdao.findOne(measuringpointid).getMeterid();
//			meterdao.updateMeter("2", meterid_old);

			// 判断terminalid
			if (terminalid == null) {
				measuringpoint.setTerminalid(measuringpointdao.findOne(measuringpointid).getTerminalid());
			} else {

				measuringpoint.setTerminalid(terminalid);
			}
			// 判断meterid
			if (meterid == null) {
				measuringpoint.setMeterid(measuringpointdao.findOne(measuringpointid).getMeterid());
			} else {

				measuringpoint.setMeterid(meterid);
			}
			// 判断collectorid
			if (collectorid == null) {
				measuringpoint.setCollectorid(measuringpointdao.findOne(measuringpointid).getCollectorid());
			} else {

				measuringpoint.setCollectorid(collectorid);
			}
			// 判断measuringpointport
			if (measuringpointport == null) {
				measuringpoint
						.setMeasuringpointport(measuringpointdao.findOne(measuringpointid).getMeasuringpointport());
			} else {

				measuringpoint.setMeasuringpointport(measuringpointport);
			}
			// 判断measuringpointbaud
			if (measuringpointbaud == null) {
				measuringpoint
						.setMeasuringpointbaud(measuringpointdao.findOne(measuringpointid).getMeasuringpointbaud());
			} else {

				measuringpoint.setMeasuringpointbaud(measuringpointbaud);
			}
			// 判断measuringpointbigint
			if (measuringpointbigint == null) {
				measuringpoint
						.setMeasuringpointbigint(measuringpointdao.findOne(measuringpointid).getMeasuringpointbigint());
			} else {

				measuringpoint.setMeasuringpointbigint(measuringpointbigint);
			}
			// 判断measuringpointsmallint
			if (measuringpointsmallint == null) {
				measuringpoint.setMeasuringpointsmallint(
						measuringpointdao.findOne(measuringpointid).getMeasuringpointsmallint());
			} else {

				measuringpoint.setMeasuringpointsmallint(measuringpointsmallint);
			}
			// 判断measuringpointint
			if (measuringpointint == null) {
				measuringpoint.setMeasuringpointint(measuringpointdao.findOne(measuringpointid).getMeasuringpointint());
			} else {

				measuringpoint.setMeasuringpointint(measuringpointint);
			}
			// 判断measuringpointdec
			if (measuringpointdec == null) {
				measuringpoint.setMeasuringpointdec(measuringpointdao.findOne(measuringpointid).getMeasuringpointdec());
			} else {

				measuringpoint.setMeasuringpointdec(measuringpointdec);
			}

			measuringpointdao.save(measuringpoint);
			// 绑定之后更改集中器状态
			if (terminalid != null) {
				terminaldao.updateTerminal("3", terminalid);
			}
//			// 绑定之后更改表状态
//			if (meterid != null) {
//				meterdao.updateMeter("3", meterid);
//			}
			// 更改采集器状态
			if (collectorid != null) {
				collectordao.updateCollector("3", collectorid);
			}

			return new JsonResult<Measuringpoint>(181, "修改采集点成功", measuringpoint);
		}
	}

	// 组装前端类
	public FileMeasuringpoint dofilemeasuringpoint(Measuringpoint measuringpoint, String languagecookie) {

		FileMeasuringpoint filemeasuringpoint = new FileMeasuringpoint();

		filemeasuringpoint.setMeasuringpointid(measuringpoint.getMeasuringpointid());
		filemeasuringpoint.setTerminalid(measuringpoint.getTerminalid());
		filemeasuringpoint.setMeterid(measuringpoint.getMeterid());
		filemeasuringpoint.setCollectorid(measuringpoint.getCollectorid());
		filemeasuringpoint.setMeasuringpointnumber(measuringpoint.getMeasuringpointnumber());
		filemeasuringpoint.setMeasuringpointordernumber(measuringpoint.getMeasuringpointordernumber());
		filemeasuringpoint.setMeasuringpointport(measuringpoint.getMeasuringpointport());
		filemeasuringpoint.setMeasuringpointportname(
				sysdicservice.finddisplay("0012", measuringpoint.getMeasuringpointport(), languagecookie));
		filemeasuringpoint.setMeasuringpointbaud(measuringpoint.getMeasuringpointbaud());
		filemeasuringpoint.setMeasuringpointbaudname(
				sysdicservice.finddisplay("0013", measuringpoint.getMeasuringpointbaud(), languagecookie));
		filemeasuringpoint.setMeasuringpointbigint(measuringpoint.getMeasuringpointbigint());
		filemeasuringpoint.setMeasuringpointbigintname(
				sysdicservice.finddisplay("0014", measuringpoint.getMeasuringpointbigint(), languagecookie));
		filemeasuringpoint.setMeasuringpointsmallint(measuringpoint.getMeasuringpointsmallint());
		filemeasuringpoint.setMeasuringpointsmallintname(
				sysdicservice.finddisplay("0015", measuringpoint.getMeasuringpointsmallint(), languagecookie));
		filemeasuringpoint.setMeasuringpointint(measuringpoint.getMeasuringpointint());
		filemeasuringpoint.setMeasuringpointintname(
				sysdicservice.finddisplay("0016", measuringpoint.getMeasuringpointint(), languagecookie));
		filemeasuringpoint.setMeasuringpointdec(measuringpoint.getMeasuringpointdec());
		filemeasuringpoint.setMeasuringpointdecname(
				sysdicservice.finddisplay("0017", measuringpoint.getMeasuringpointdec(), languagecookie));

		return filemeasuringpoint;
	}

	// 通过终端地址查下面第一个表的表协议
	public String findMeterprotocolByTerminaladdress(String terminaladdress) {
		Integer terminalid = terminaldao.findByTerminaladdress(terminaladdress).getTerminalid();
		if (measuringpointdao.findByTerminalid(terminalid).size() != 0) {
			Integer meterid = measuringpointdao.findByTerminalid(terminalid).get(0).getMeterid();
			String meterprotocol = meterdao.findOne(meterid).getMeterprotocol();
			return meterprotocol;
		} else
			return null;
	}

}
